﻿<?php

class OrderApp extends BaseApp
{
    function index()
    {
        if(!isset($_POST['keyword']))
		{
		$_POST['keyword'] = '';
		}
        if(!isset($_POST['type']))
		{
		$_POST['type'] = '';
		}
        if(!isset($_POST['status']))
		{
		$_POST['status'] = '';
		}
        if(!isset($_GET['keyword']))
		{
		$_GET['keyword'] = '';
		}
        if(!isset($_GET['type']))
		{
		$_GET['type'] = '';
		}
        if(!isset($_GET['status']))
		{
		$_GET['status'] = '';
		}
		$keyword=$_POST['keyword']==''?$_GET['keyword']:$_POST['keyword'];

		$searchtype=$_POST['type']==''?$_GET['type']:$_POST['type'];

		$status=$_POST['status']==''?$_GET['status']:$_POST['status'];


		 	
		$TB_leaveword = &$this->_model('order');  				
		$data = $TB_leaveword->show($keyword,$searchtype,$status);	

        $this->assign('lw_data', $data['data']);		
		$this->assign('lw_page', $data['page']);				

        $this->assign('searchtype', $searchtype);		
        $this->assign('status', $status);	
        $this->assign('keyword', $keyword);	
					    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
		
        $this->display('order_list.view.html');
    }


	function confirmorder()
	{
		$id=$_GET['id'];

		 	
		$TB_order = &$this->_model('order');  				
		$order = $TB_order->confirmorder($id);	

        $this->assign('order', $order);
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
		
        $this->display('order_confirm.view.html');

	}

	function doconfirmorder()
	{
        $type=$_POST['type'];
        $money=$_POST['money'];
        $number=$_POST['number'];
        $orderid=$_POST['orderid'];

		if($type=='' || intval($money)=='' || $number=='')
		{
		 $notice = &get_message(array('msg'=>'您必须详细记录所有交易信息!','url'=>'','title'=>'Notice'));
		 $notice ->display();	
		}

		if($money<=0)
		{
		 $notice = &get_message(array('msg'=>'错误，交易金额必须是一个大于0的整数!','url'=>'','title'=>'Notice'));
		 $notice ->display();	
		}			

		$money=intval($money);

		$TB_order = &$this->_model('order');  				
		$order = $TB_order->orderget($orderid);	

		if($order['pay']==1)
		{
		 $notice = &get_message(array('msg'=>'错误，该订单已经支付过了!','url'=>'','title'=>'Notice'));
		 $notice ->display();	
		}			

		$TB_order->moneyadd($money,$order['userid']);

		$ary=array(
			'userid' => $order['userid'],
			'type' => 1,
			'name' => '充值(后台)',
			'intro' => '您充值了'.$money.'元，由管理员（'.$GLOBALS['SCup']->SCup_user_msg['data']['user_name'].'）确认收到货款',
			'money' => $money,
			'time' => time(),
		);

		$TB_order->moneylog($ary);

		$product = $TB_order->productcheck($order['productid']);

		if($product == false)
		{
		$notice = &get_message(array('msg'=>'错误~该产品不存在或已结束团购!','url'=>'','title'=>'Notice'));
	    $notice ->display();

		}
		if($product['maxnum']!=0)
		{
			$paytotal=$TB_order->paytotal($order['groupid']);
            
			$payroductnum = $paytotal['payroductnum'];

			if($product['maxnum']-$payroductnum<=$product['productnum'])
			{
	         	$notice = &get_message(array('msg'=>'该产品被人抢先购买了，钱已经充值到个人账户！','url'=>'','title'=>'Notice'));
	            $notice ->display();
			}
		}

       if(isset($product['bookprice'])&&$product['bookprice']!=0)
	   {
		$price=$product['bookprice']*$order['productnum'];
	   }
	   else
	   {
		$price=$product['nowprice']*$order['productnum'];	   
	   }

		$money=$TB_order->moneyuser($order['userid']);

		if($price<=$money)
		{
			$result=$TB_order->moneypay($price,$order['userid']);
			$TB_order->addsellertomoney($product['sellerid'],$price);

		}else
		{
		   $notice = &get_message(array('msg'=>'余额不足，不够支付本次消费！','url'=>'','title'=>'Notice'));
		   $notice ->display();
		}

		$result=$TB_order->ordertype($orderid,1,1,1);

		$this->ticket($orderid);

		$notice = &get_message(array('msg'=>'订单修改成功！','url'=>'','title'=>'Notice'));
	    $notice ->display();
	}


	function mailcallpay()
	{
	     $id = $_GET['id'];
		 $TB_order = &$this->_model('order'); 
         $order= $TB_order->mailcallpay($id);

		$ary=array(
		'address'=> $order['user_email'],
		'username'=>$order['user_name'],
		'title'=>'订单支付温馨提示',
		'content'=>'温馨提示您团购的产品（'.$order['name'].')即将结束,而您还没有付款，想拥有现在是好机会啊，请点<a href="'.SITE_URL.'">这里完成付款立刻拥有该商品</a>！',
		'addtime'=>time()
		);

        $TB_order->mailCron($ary);
		$notice = &get_message(array('msg'=>'邮件通知成功!','url'=>'','title'=>'Notice'));
	    $notice ->display();

	}
	

	function warnofticket()
	{
	     $id = $_GET['id'];
		$id=intval($id);

		 $TB_order = &$this->_model('order'); 
         $order= $TB_order->warnofticket($id);

		$ary=array(
		'address'=> $order['user_email'],
		'username'=>$order['user_name'],
		'title'=>'购物券即将到期提示信息',
		'content'=>'温馨提示您购买的产品（'.$order['name'].'）消费券即将于'.$order['perioddate'].'到期请尽快消费以免过期，请点<a href="'.SITE_URL.'">这里</a>查看您的团购券！',
		'addtime'=>time()
		);
        $TB_order->mailCron($ary);
		$notice = &get_message(array('msg'=>'您已经成功提醒了该用户！','url'=>'','title'=>'Notice'));
	    $notice ->display();

	}
	



    function ticketz()
	{
        if(!isset($_POST['keyword']))
		{
		$_POST['keyword'] = '';
		}
        if(!isset($_POST['type']))
		{
		$_POST['type'] = '';
		}
        if(!isset($_POST['time']))
		{
		$_POST['time'] = '';
		}
        if(!isset($_POST['used']))
		{
		$_POST['used'] = '';
		}		
        if(!isset($_GET['keyword']))
		{
		$_GET['keyword'] = '';
		}
        if(!isset($_GET['type']))
		{
		$_GET['type'] = '';
		}
        if(!isset($_GET['time']))
		{
		$_GET['time'] = '';
		}
        if(!isset($_GET['used']))
		{
		$_GET['used'] = '';
		}		
		$keyword=$_POST['keyword']==''?$_GET['keyword']:$_POST['keyword'];

		$searchtype=$_POST['type']==''?$_GET['type']:$_POST['type'];

		$time=$_POST['time']==''?$_GET['time']:$_POST['time'];
		$used=$_POST['used']==''?$_GET['used']:$_POST['used'];

		$TB_order = &$this->_model('order');  				
		$data = $TB_order->allticket($keyword,$searchtype,$time,$used);

        $this->assign('lw_data', $data['data']);		
		$this->assign('lw_page', $data['page']);				
	

        $this->assign('keyword', $keyword);		
        $this->assign('searchtype', $searchtype);	
        $this->assign('time', $time);	
        $this->assign('used', $used);	


        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
		
        $this->display('ticket_list.view.html');

	}


	function ticket($orderid)
	{			
	   $TB_order = &$this->_model('order');		 
	   $order = $TB_order->orderticket($orderid);	

		if($order=='')
		{
		 $notice = &get_message(array('msg'=>'错误，无法找到类似订单！','url'=>'?','title'=>'Notice'));
		 $notice ->display();		
		}

       if(isset($order['bookprice'])&&$order['bookprice']!=0)
	   {
	   $order['nowprice'] = $order['bookprice'];
	   }

		$ary=array(
			'userid' => $order['ouid'],
			'type' => 0,
			'name' => '团购商品',
			'intro' => '购买了商品['.$order['name'].'] 单价：'.$order['nowprice'].'元     一共购买了'.$order['productnum'].'个',
			'money' => $order['nowprice']*$order['productnum'],
			'time' => time(),
		);

		$TB_order->moneylog($ary);

		$product = $TB_order->paytotal($order['groupid']);
		
		$num=$product['totalorder'];

		if($num<$order['successnum'])
		{

		 $notice = &get_message(array('msg'=>'您已经成功团购该产品，但团购差'.($order['successnum']-$num).'人达成 ，快去邀请好友参加团购，还能获得丰厚获取奖励!','url'=>'?','title'=>'Notice'));
		 $notice ->display();	

		}
		
		if($num==$order['successnum'])
		{
			$orderPayed=$TB_order->orderPaylist($order['groupid']);

			foreach($orderPayed as $key => $value)
			{

				$TB_order->producttypedit($value['productid'],2);
				$ary=array(
				'address'=> $value['email'],
				'username'=>$value['username'],
				'title'=>'团购成功提示信息',
				'content'=>'恭喜您在本站团购的产品('.$order['name'].')在'.date('Y-m-d H:i',time()).'团购成功，请点<a href="'.SITE_URL.'">这里</a>查看您的团购券！',
				'addtime'=>time()
				);

		        $TB_order->mailCron($ary);

				for($i=1;$i<=$value['productnum'];$i++)
				{
					$result=$TB_order->ticketCreate($value['userid'],$value['productid'],$order['orderid'],$order['groupid']);
					if($result=='')$i--;
				}
			}

		    $notice = &get_message(array('msg'=>'团购成功，您还可以推荐您的朋友来购买哦！','url'=>'?','title'=>'Notice'));
		    $notice ->display();	

		}

		
		$TB_order->producttypedit($order['productid'],2);
		for($i=1;$i<=$order['productnum'];$i++)
		{
			$result=$TB_order->ticketCreate($order['ouid'],$order['productid'],$order['orderid'],$order['groupid']);
			if($result=='')$i--;
		}
	    $notice = &get_message(array('msg'=>'团购成功，您还可以推荐您的朋友来购买哦！','url'=>'?','title'=>'Notice'));
	    $notice ->display();

	}

    function deleteticket()
	{
	     $id = $_GET['ticketid'];
		 $TB_order = &$this->_model('order'); 

		 $TB_order->deleteticket($id);	
		  
	    $notice = &get_message(array('msg'=>'删除成功','url'=>'?app=order&act=ticketz','title'=>'Notice'));
	    $notice ->display(); 
	}

    function deleteorder()
    {
	     $id = $_GET['id'];
		 $TB_order = &$this->_model('order'); 	 
			 
		 $TB_order->del($id);	
		  
	    $notice = &get_message(array('msg'=>'操作成功','url'=>'?app=order&act=index','title'=>'Notice'));
	    $notice ->display();  

    }


    function mainpay()
	{

		 $TB_order = &$this->_model('order'); 	 
			 
		 $pay=$TB_order->getallpay();
        $this->assign('pay', $pay);

        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
		
        $this->display('pay_list.view.html');

	}

   function onlinepay()
	{
	$id = intval($_GET['id']);

		 $TB_order = &$this->_model('order'); 	 
			 
		 $TB_order->updateonlinepay($id);


	    $notice = &get_message(array('msg'=>'操作成功','url'=>'?app=order&act=mainpay','title'=>'Notice'));
	    $notice ->display();  

	}

    function setpay()
	{
	$id = intval($_GET['id']);

	$TB_order = &$this->_model('order'); 	
	$pay=$TB_order->getonepay($id);

	$cfg_value=unserialize($pay['pay_config']);

        $this->assign('pay', $pay);
		$this->assign('cfg_value', $cfg_value);

        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
		
        $this->display('pay_'.$pay['pay_code'].'.view.html');

	}
	
   function dosetpay()
  {

    $pay_desc = $_POST['pay_desc'];
	$pay_order     = $_POST['pay_order'];
	$cfg_value  = $_POST['cfg_value'];
	$mid  = $_POST['mid'];

		$ary=array(

		'pay_desc' => $pay_desc,
		'pay_order' => $pay_order,
		'pay_config' => serialize($cfg_value), 
	);

		 $TB_order = &$this->_model('order'); 	 
			 
		 $TB_order->dosetpay($ary,$mid);


	    $notice = &get_message(array('msg'=>'操作成功','url'=>'?app=order&act=mainpay','title'=>'Notice'));
	    $notice ->display();  
	}

   
}




?>